package com.jacen.fzxs.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jacen.fzxs.entity.DictItemEntity;
import com.jacen.fzxs.service.IDictItemService;
import com.jacen.fzxs.service.IWallCustomerService;
import com.jacen.fzxs.util.EntityUtils;
import com.jacen.fzxs.util.ServletUtils;
import com.jacen.fzxs.util.Utils;

/**
 * 字典
 * @author Jacen
 *
 */
@Controller
public class DictItemController extends BaseController {
	
	@Resource
	private IDictItemService itemService;
	
	@Resource
	private IWallCustomerService wcService;
	
	private static Logger logger = LoggerFactory.getLogger(DictItemController.class);
	
	public static Map<String, String> trade = new HashMap<String, String>();
	static{
		trade.put("groupName", "trade");
	}
	
	/**
	 * 获取列表
	 * @param request
	 * @param itemValue
	 * @param modelMap
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping("/item/list.do")
	public String getList(HttpServletRequest request,String itemValue,ModelMap modelMap) throws Exception{
		Map<String, String> params = ServletUtils.getParameterMap(request);
		List<DictItemEntity> list = itemService.getList(params);
		modelMap.addAttribute("list", list);
		modelMap.addAttribute("itemValue", itemValue);
		logger.info("用户："+getLocalUser(request).getUserName()+"查看行业列表");
		return "dictitem/list";
	}
	
	/**
	 * 进入新增页面
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/item/insert.do",method = RequestMethod.GET)
	public String enterInsert(HttpServletRequest request,ModelMap modelMap){
		Map<String, String> tradeMap = new HashMap<String, String>();
		tradeMap.put("groupName", "trade");
		List<DictItemEntity> list = itemService.getList(tradeMap);
		modelMap.put("list", list);
		logger.info("用户："+getLocalUser(request).getUserName()+"进入新增行业页面");
		return "dictitem/insert";
	}
	
	/**
	 * 新增
	 * @param request
	 * @param itemEntity
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/item/insert.do",method = RequestMethod.POST)
	@ResponseBody
	public String postInsert(HttpServletRequest request,DictItemEntity itemEntity) throws Exception{
		boolean flag = itemService.existByKey("itemValue", itemEntity.getItemValue());
		if(flag) return Utils.errorJsonResp("该行业已存在，请重新输入");
		itemEntity.setId(EntityUtils.createId32());
		itemService.insert(itemEntity);
		logger.info("用户："+getLocalUser(request).getUserName()+"新增行业:"+itemEntity.getItemValue());
		return Utils.normalJsonResp();
	}
	
	/**
	 * 进入修改页面
	 * @param request
	 * @param id
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/item/update.do",method = RequestMethod.GET)
	public String enterUpdate(HttpServletRequest request,String id,ModelMap modelMap){
		DictItemEntity item  = itemService.getById(id);
		modelMap.addAttribute("item", item);
		logger.info("用户："+getLocalUser(request).getUserName()+"进入修改行业页面");
		return "dictitem/update";
	}
	
	/**
	 * 修改行业
	 * @param request
	 * @param itemEntity
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/item/update.do",method = RequestMethod.POST)
	public String postUpdate(HttpServletRequest request,DictItemEntity itemEntity) throws Exception{
		itemService.update(itemEntity);
		logger.info("用户："+getLocalUser(request).getUserName()+"修改行业信息："+itemEntity.getItemValue());
		return "redirect:/item/list.do?groupName=trade";
	}
	
	/**
	 * 删除行业
	 * @param request
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/item/delete.do")
	@ResponseBody
	public String delete(HttpServletRequest request,String id) throws Exception{
		DictItemEntity itemEntity = itemService.getById(id);
		boolean flag = wcService.existByKey("trade", itemEntity.getItemName());
		if(flag){
			return Utils.errorJsonResp("该行业已经应用于系统中，不能删除!");
		}
		itemService.deleteById(id);
		logger.info("用户："+getLocalUser(request).getUserName()+"删除行业,id="+id);
		return Utils.normalJsonResp();
	}
	
	/**
	 * 批量删除行业
	 * @param request
	 * @param ids
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping("/item/batchDelete.do")
	@ResponseBody
	public String batchDelete(HttpServletRequest request , String[] ids) throws Exception{
		DictItemEntity itemEntity = null;
		int i = 0;
		boolean flag = false;
		for(String id : ids){
			itemEntity = itemService.getById(id);
			flag = wcService.existByKey("trade", itemEntity.getItemName());
			if(flag)continue;
			itemService.deleteById(id);
			i++;
		}
		if(i==0){
			return Utils.errorJsonResp("所选行业均应用于系统中，不能删除!");
		}
		logger.info("用户："+getLocalUser(request).getUserName()+"批量删除行业,id="+ids.toString());
		return Utils.normalJsonResp();
	}
}


